package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.controller.form.CondFrom;
import com.woniuxy.controller.form.ProjectIdNamePageForm;
import com.woniuxy.controller.form.ProjectNewFrom;
import com.woniuxy.controller.form.ProjectStateForm;
import com.woniuxy.controller.info.ProjectIdAndNameInfo;
import com.woniuxy.controller.info.ProjectInfo;
import com.woniuxy.dao.ProjectRoleUserDao;
import com.woniuxy.controller.info.UserAndAccountInfo;
import com.woniuxy.dao.ProjectRoleUserDao;
import com.woniuxy.model.Project;
import com.woniuxy.dao.ProjectDao;
import com.woniuxy.model.ProjectRoleUser;
import com.woniuxy.service.ProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.utils.CopyUtil;
import com.woniuxy.utils.TimeMethodsutil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 马世亮
 * @since 2022-02-15
 */
@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectDao, Project> implements ProjectService {

    @Resource
    private ProjectDao projectDao;
    @Resource
    private ProjectRoleUserDao projectRoleUserDao;

    @Transactional
    @Override
    public boolean addNewProject(ProjectNewFrom projectNewFrom) {

        Project project = new Project();
        project.setProjectName(projectNewFrom.getProjectname());
        project.setProjectDescription(projectNewFrom.getProjectdescribe());
        project.setProjectState(1);
        project.setProjectCreatenameId(projectNewFrom.getUid());
        project.setProjectCreatname(projectNewFrom.getUname());
        project.setProjectCreatetime(TimeMethodsutil.getNowTimeToString());

        int insert = projectDao.insert(project);
        if(insert<=0) return false;

        ProjectRoleUser projectRoleUser = new ProjectRoleUser();
        projectRoleUser.setProjectId(project.getProjectId());
        projectRoleUser.setUId(projectNewFrom.getUid());
        projectRoleUser.setProjectRoleId(projectNewFrom.getProjectRoleId());
        int insert1 = projectRoleUserDao.insert(projectRoleUser);
        if(insert1<=0) return false;

        return true;
    }

    @Override
    public IPage<ProjectInfo> getprojects(CondFrom condFrom) {

        Page<Project> liPage = new Page<>(condFrom.getPagenum() == 0 ? 1 : condFrom.getPagenum(), condFrom.getPagesize() == 0 ? 10 : condFrom.getPagesize());

        List<Project> projects = null;
        if(null == condFrom.getCondition() || condFrom.getCondition().equals("")){
            System.out.println(1111);
            QueryWrapper<Project> wrapper = new QueryWrapper<>();
            wrapper.eq("project_createname_id",condFrom.getUid());
            IPage<Project> projectPage = projectDao.selectPage(liPage, wrapper);
            projects = projectPage.getRecords();
        }else{
            System.out.println(2222);
            projects = projectDao.getProjects(condFrom.getCondition(), condFrom.getUid());
        }

        List<ProjectInfo> projectInfos = CopyUtil.copyList(projects, ProjectInfo.class);
        for (int i = 0; i < projects.size(); i++) {
            if(projects.get(i).getProjectState()==0){
                projectInfos.get(i).setProjectState("关闭");
            }else if(projects.get(i).getProjectState()==1){
                projectInfos.get(i).setProjectState("有效");
            }
        }

        Page<ProjectInfo> liInfoPage = new Page<>();
        liInfoPage.setCurrent(liPage.getCurrent());
        liInfoPage.setRecords(projectInfos);
        liInfoPage.setSize(liPage.getSize());
        liInfoPage.setTotal(liPage.getTotal());

        return liInfoPage;
    }

    @Override
    public boolean changeproject(ProjectNewFrom projectNewFrom) {

        Project project = new Project();
        project.setProjectName(projectNewFrom.getProjectname());
        project.setProjectDescription(projectNewFrom.getProjectdescribe());
        project.setProjectChangename(projectNewFrom.getUname());
        project.setProjectChangetime(TimeMethodsutil.getNowTimeToString());

        QueryWrapper<Project> wrapper = new QueryWrapper<>();
        wrapper.eq("project_id",projectNewFrom.getProjectid());
        int update = projectDao.update(project, wrapper);
        return update>0? true:false;
    }

    @Override
    public boolean changestate(ProjectStateForm projectStateForm) {

        Project project = new Project();
        if(projectStateForm.getProjectState().equals("有效")){
            project.setProjectState(0);
        }else if(projectStateForm.getProjectState().equals("关闭")){
            project.setProjectState(1);
        }
        System.out.println(project);
        QueryWrapper<Project> wrapper = new QueryWrapper<>();
        wrapper.eq("project_id",projectStateForm.getProjectid());
        int update = projectDao.update(project, wrapper);
        return update>0? true:false;
    }

    @Override
    public Page<ProjectIdAndNameInfo> getAllProjectByPage(ProjectIdNamePageForm projectIdNamePageForm) {

        Page<Project> projectPage = projectDao.selectPage(new Page<Project>(projectIdNamePageForm.getPageNum(), projectIdNamePageForm.getPageSize()), new QueryWrapper<Project>().eq("project_state","1"));
        List<Project> projects = projectPage.getRecords();
        List<ProjectIdAndNameInfo> projectIdAndNameInfos = CopyUtil.copyList(projects, ProjectIdAndNameInfo.class);

        Page<ProjectIdAndNameInfo> projectIdAndNameInfoPage = new Page<>();
        projectIdAndNameInfoPage.setRecords(projectIdAndNameInfos);
        projectIdAndNameInfoPage.setCurrent(projectPage.getCurrent());
        projectIdAndNameInfoPage.setSize(projectPage.getSize());
        projectIdAndNameInfoPage.setTotal(projectIdAndNameInfoPage.getTotal());
        return projectIdAndNameInfoPage;
    }

    @Override
    public List<ProjectIdAndNameInfo> getMyProjectName(Integer uid) {
        List<ProjectIdAndNameInfo> projectIdAndNameInfos = this.baseMapper.selectMyProjectByUidAndState(uid);
        return projectIdAndNameInfos;
    }

    @Override
    public List<ProjectIdAndNameInfo> getHaveMyProjectName(Integer uid) {
        List<ProjectRoleUser> projectRoleUsers = projectRoleUserDao.selectList(new QueryWrapper<ProjectRoleUser>().eq("u_id", uid));
        if(projectRoleUsers==null||projectRoleUsers.size()==0){
            return null;
        }
        List<Integer> projectIds=new ArrayList<Integer>();
        for (ProjectRoleUser projectRoleUser : projectRoleUsers) {
            projectIds.add(projectRoleUser.getProjectId());
        }
        List<ProjectIdAndNameInfo> projectIdAndNameInfos=projectDao.selectHaveMyProjectByProjectList(projectIds);

        return projectIdAndNameInfos;
    }

    @Override
    public List<Project> getAllprojectName() {
        List<Project> allprojectName = projectDao.getAllprojectName();
        return allprojectName;
    }


}
